package com.Sdebruijn.curriculumvitae.models;

import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * Class describing the institution where {@link Experience} has been provided
 * by. Currently either a school or company.
 * 
 * @author Stefan de Bruijn
 */
@DatabaseTable(tableName = "Institution")
public class Institution implements Parcelable {

	Institution() {
		// all persisted classes must define a no-arg constructor with at least
		// package visibility
	}

	/**
	 * Auto-incremented id for the ORMLite-SQLite database
	 */
	@DatabaseField(generatedId = true)
	private int id;

	/**
	 * {@link String} describing the full name of the institution
	 */
	@DatabaseField
	@SerializedName("name")
	public String name;

	/**
	 * {@link String} describing the short name of the institution
	 */
	@DatabaseField
	@SerializedName("name_short")
	public String name_short;

	/**
	 * {@link String} describing the institution shortly
	 */
	@DatabaseField
	@SerializedName("description")
	public String description;

	/**
	 * {@link String} containing the URL to the website
	 */
	@DatabaseField
	@SerializedName("website")
	public String website; // TODO ORMLite/Gson can't handle URL class?

	/**
	 * {@link Person} linking this institution to a possible reference.
	 */
	@DatabaseField(foreign = true, columnName = "reference_id")
	@SerializedName("reference")
	public Reference reference;

	/**
	 * {@link String} containing the postal address
	 */
	@DatabaseField
	@SerializedName("address")
	public String address; // TODO ORMLite/Gson can't handle Address class?

	/**
	 * {@link String} containing the URL to the institution's logo
	 */
	@DatabaseField
	@SerializedName("logo")
	public String logo;

	/*
	 * Required methods to have this class as Parcelable Having this class
	 * Parcelable will make it easier to restore it into fragments with
	 * savedInstanceState
	 */

	/*
	 * Just used as id - hashCode() will do fine
	 * 
	 * @see android.os.Parcelable#describeContents()
	 */
	@Override
	public int describeContents() {
		return this.hashCode();
	}

	/*
	 * Serialize my object attributes one by one
	 * 
	 * @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
	 */
	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeInt(this.id);
		parcel.writeString(this.address);
		parcel.writeString(this.description);
		parcel.writeString(this.logo);
		parcel.writeString(this.name);
		parcel.writeString(this.name_short);
		parcel.writeString(this.website);
		parcel.writeString(this.name);

		parcel.writeParcelable(this.reference, 0);
	}

	/**
	 * Required for any Parcelable class to be recreated. Will be called to
	 * de-seralize the object.
	 * 
	 * @author Stefan de Bruijn
	 * 
	 */
	public static final Parcelable.Creator<Institution> CREATOR = new Parcelable.Creator<Institution>() {
		public Institution createFromParcel(Parcel source) {
			return new Institution(source);
		}

		public Institution[] newArray(int size) {
			return new Institution[size];
		}
	};

	/**
	 * <b>Do not use!</b> Private constructor for the Parcelable Creator. Will
	 * de-serialize the object.
	 * 
	 * @param source
	 */
	private Institution(Parcel source) {
		this.id = source.readInt();
		this.address = source.readString();
		this.description = source.readString();
		this.logo = source.readString();
		this.name = source.readString();
		this.name_short = source.readString();
		this.website = source.readString();

		this.reference = source.readParcelable(Reference.class.getClassLoader());

	}

}
